<?php

Class MemberFavorModel extends Model
{

    /**
     * 获得单条数据
     * @param $condition 条件
     * @return mixed
     */
    public function getfavorByOne($condition, $field)
    {
        return $this->where($condition)->field($field)->find();
    }

    /**
     * 获得多条数据
     * @param $condition 条件
     * @param $field 字段
     * @param $order 排序
     * @param $limit 数量
     * @return mixed
     */
    public function getfavorByMore($condition, $field, $order, $limit)
    {
        return $this->where($condition)->field($field)->order($order)->limit($limit)->select();
    }

    /**
     * @param $data 数据
     * @return mixed|void
     */
    public function insert($data)
    {
        if (empty($data)) {
            return;
        }
        return $this->add($data);
    }

    /**
     * 更新数据
     */
    public function updata($condition, $data)
    {
        return $this->where($condition)->save($data);
    }

     /**
      * 添加喜欢
      */
    public function addFovar($data)
    {
        $cid = $data['cid'];
        $pid = $data['postid'];
        if ($cid == '' || $pid == '') {
            return ['status' => 400, 'msg' => "参数不能为空"];
        }

        $where['uid'] = session('uid');
        $where['fclassid'] = $cid;
        $where['pfavortidid'] = $pid;
        $is = $this->getfavorByOne($where);
        if($is){
            return ['status' => 400, 'msg' => "该文章已经添加喜欢分类"];
        }
        $inserData['uid'] = session('uid');
        $inserData['fclassid'] = $cid;
        $inserData['favortid'] = $pid;
        $inserData['created_at'] = time();
        $is_add = $this->insert($inserData);
        if($is_add){
            return ['status' => 200, 'msg' => "喜欢成功"];
            M("article")->where(array('id'=>$pid))->setInc("love",1);
//            D('article')->setFiledInc(array('id'=>$pid),'love');
        }
        return ['status' => 400, 'msg' => "喜欢失败"];
    }

    /**
     * 移除喜欢
     */
    public function removeFavor($data){
          $id = $data['id'];
          $is = $this->getfavorByOne(array('id'=>$id));
          if(!$is){
              return ['status' => 400, 'msg' => "参数错误"];
          }
          $is_remove = $this->where(array('id'=>$id))->delete();
          if($is_remove){
              M("article")->where(array('id'=>$is['favortid']))->setDec("love",1);
              return ['status' => 200, 'msg' => "取消喜欢成功"];
          }
          return ['status' => 400, 'msg' => "取消喜欢失败"];
    }
}